package com.alipay.sofa.demo.db.core.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import com.alipay.sofa.demo.db.common.entity.StudentInfo;
import com.alipay.sofa.demo.db.common.exception.OperateDataException;
import com.alipay.sofa.demo.db.common.service.StudentInsertService;
import com.alipay.sofa.demo.db.core.dao.StudentInfoMapper;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 插入学生数据接口实现类
 * @Author Zhang Da
 * @Date 2020/2/13 16:08
 * @Version 1.0
 */
@SofaService(interfaceType = StudentInsertService.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Component
public class StudentInsertServiceImpl implements StudentInsertService {
    @Resource
    StudentInfoMapper studentInfoMapper;

    @Override
    public int insertStudents(StudentInfo studentInfo) throws OperateDataException {
        if(StringUtils.isBlank(studentInfo.getStudentNo())){
            throw new OperateDataException("student no can not be null");
        }
        return studentInfoMapper.insert(studentInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertStudents(List<StudentInfo> studentInfoList) throws OperateDataException {

        return studentInfoMapper.insertBatch(studentInfoList);
    }
}
